<template>
  <div class="score-container">
    <!-- 顶部功能区 -->
    <div class="header-section">
      <div class="filter-group">
        <select class="filter-select">
          <option>全部课程</option>
          <option>高等数学</option>
          <option>大学英语</option>
          <option>计算机基础</option>
        </select>
        <select class="filter-select">
          <option>全部班级</option>
          <option>一班</option>
          <option>二班</option>
          <option>三班</option>
        </select>
        <div class="total-score">平均分: {{ avgScore }}</div>
      </div>
      <div class="action-group">
        <button class="login-btn">登录</button>
        <input type="text" class="search-input" placeholder="搜索学生...">
      </div>
    </div>

    <!-- 课程信息区 -->
    <div class="course-info">
      <h2>当前课程: 高等数学</h2>
      <p>最高分: {{ maxScore }} | 最低分: {{ minScore }} | 授课教师: 张教授</p>
    </div>

    <!-- 学生成绩表格 -->
    <div class="score-table">
      <table>
        <thead>
          <tr>
            <th>学号</th>
            <th>学生姓名</th>
            <th>班级</th>
            <th>评语</th>
            <th>得分</th>
            <th>回答</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="student in students" :key="student.id" 
              @mouseover="hoverRow = student.id" 
              @mouseleave="hoverRow = null" 
              :class="{ 'hover-row': hoverRow === student.id }">
            <td>{{ student.id }}</td>
            <td>{{ student.name }}</td>
            <td>{{ student.class }}</td>
            <td>{{ student.comment }}</td>
            <td>{{ student.score }}</td>
            <td>
              <button class="view-btn" @click="viewAnswers(student.id)">查看</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    const hoverRow = ref(null);
    const students = ref([
      { id: '1001', name: '张三', class: '一班', comment: '表现优秀', score: 95 },
      { id: '1002', name: '李四', class: '一班', comment: '作业完成良好', score: 88 },
      { id: '1003', name: '王五', class: '二班', comment: '积极参与课堂', score: 90 },
      { id: '1004', name: '赵六', class: '一班', comment: '基础知识扎实', score: 92 },
      { id: '1005', name: '钱七', class: '三班', comment: '需要加强练习', score: 78 },
      { id: '1006', name: '孙八', class: '二班', comment: '进步明显', score: 85 },
      { id: '1007', name: '周九', class: '一班', comment: '思维活跃', score: 89 },
      { id: '1008', name: '吴十', class: '三班', comment: '按时完成作业', score: 82 },
      { id: '1009', name: '郑十一', class: '二班', comment: '课堂表现积极', score: 91 },
      { id: '1010', name: '王十二', class: '一班', comment: '学习态度端正', score: 87 }
    ]);

    const scores = ref([
      { courseName: '高等数学', score: 85 },
      { courseName: '大学英语', score: 92 },
      { courseName: '计算机基础', score: 88 }
    ]);

    const avgScore = computed(() => {
      const sum = scores.value.reduce((acc, cur) => acc + cur.score, 0);
      return Math.round(sum / scores.value.length);
    });

    const maxScore = computed(() => {
      return Math.max(...scores.value.map(item => item.score));
    });

    const minScore = computed(() => {
      return Math.min(...scores.value.map(item => item.score));
    });

    const viewAnswers = (id) => {
      console.log('查看学生回答:', id);
    };

    return {
      students,
      hoverRow,
      avgScore,
      maxScore,
      minScore,
      viewAnswers
    };
  }
};
</script>

<style scoped>
.score-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 15px;
}

.filter-select {
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid #ddd;
}

.total-score {
  font-weight: bold;
  color: #2c3e50;
}

.action-group {
  display: flex;
  align-items: center;
  gap: 15px;
}

.login-btn {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.login-btn:hover {
  background-color: #3aa876;
}

.search-input {
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid #ddd;
  min-width: 200px;
}

.course-info {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.course-info h2 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.course-info p {
  margin: 0;
  color: #666;
}

.score-table {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

th {
  background-color: #f2f2f2;
  font-weight: 600;
}

tr:hover {
  background-color: #f5f5f5;
}

.hover-row {
  background-color: #e9f7ef;
}

.view-btn {
  padding: 6px 12px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.view-btn:hover {
  background-color: #2980b9;
}

@media (max-width: 768px) {
  .header-section {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .filter-group, .action-group {
    width: 100%;
    flex-wrap: wrap;
  }
  
  .filter-select, .search-input {
    width: 100%;
    margin-bottom: 10px;
  }
  
  th, td {
    padding: 8px;
    font-size: 14px;
  }
}
</style>